package com.cdwang.notification_module;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;

import androidx.core.app.NotificationCompat;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * copyright (C), 2022, 运达科技有限公司
 * fileName NotificationUtils
 *
 * @author 王玺权
 * date 2022-10-21 15:20
 * description
 * history
 */
public class NotificationUtils {
    private static String mNormalChannelId = "渠道id"; // 唯一性
    private  static String mNormalChannelName = "渠道名称";

    private static String mHighChannelId = "重要渠道id";
    private static String mHighChannelName = "重要通知";

    private static String mProgressChannelId = "进度条渠道id";
    private static String mProgressChannelName = "进度条通知";

    private static String mBigTextChannelId = "大文本渠道id";
    private static String mBigTextChannelName = "大文本通知";

    private static String mBigImageChannelId = "大图片渠道id";
    private static String mBigImageChannelName = "大图片通知";

    private static String mCustomChannelId = "自定义渠道id";
    private static String mCustomChannelName = "自定义通知";
    private static NotificationManager mManager;
    private static NotificationCompat.Builder mBuilder;

    private static int mNormalNotificationId = 9001; // 通知id
    private static int mHighNotificationId = 9002; // 通知id
    private static int mBigTextNotificationId = 9003 ;// 通知id
    private static int mProgressNotificationId = 9004; // 通知id
    private static int mBigImageNotificationId = 9005; // 通知id
    private static int mCustomNotificationId = 9006; // 通知id



    public static void createNotificationForNormal(Activity currentActivity,Class toClass,int res
    ,String title,String content
    ) {

        if(mManager==null){
            mManager= (NotificationManager) currentActivity.getSystemService(NOTIFICATION_SERVICE);
        }

        // 适配8.0及以上 创建渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel=new NotificationChannel(mNormalChannelId,mNormalChannelName,NotificationManager.IMPORTANCE_HIGH);
            String description = "描述";
            channel.setDescription(description);
            channel.setShowBadge(true); // 是否在桌面显示角标
            mManager.createNotificationChannel(channel);
        }
        // 点击意图 // setDeleteIntent 移除意图
        Intent intent = new Intent(currentActivity,toClass);

        PendingIntent pendingIntent=null;

        if(currentActivity!=null&&toClass!=null){
            pendingIntent = PendingIntent.getActivity(currentActivity, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        }

        // 构建配置
        mBuilder = new NotificationCompat.Builder(currentActivity, mNormalChannelId)
                .setContentTitle(title) // 标题
                .setContentText(content) // 文本
                .setSmallIcon(res) // 小图标
                // 向通知添加声音、闪灯和振动效果
//                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_ALL | Notification.DEFAULT_SOUND)
                .setDefaults(Notification.DEFAULT_VIBRATE)
                .setLargeIcon(BitmapFactory.decodeResource(currentActivity.getResources(), res)) // 大图标
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 7.0 设置优先级
                .setContentIntent(pendingIntent) // 跳转配置

                .setCategory(NotificationCompat.CATEGORY_MESSAGE) // 通知类别，"勿扰模式"时系统会决定要不要显示你的通知
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE) // 屏幕可见性，锁屏时，显示icon和标题，内容隐藏

                .setAutoCancel(true); // 是否自动消失（点击）or mManager.cancel(mNormalNotificationId)、cancelAll、setTimeoutAfter()
        // 发起通知
        mManager.notify(mNormalNotificationId, mBuilder.build());
    }


    public static void createNotificationForHigh(Activity currentActivity,Class toClass,int res
            ,String title,String content) {
        if(mManager==null){
            mManager= (NotificationManager) currentActivity.getSystemService(NOTIFICATION_SERVICE);
        }
//        Intent intent = new Intent(currentActivity, toClass);
//        PendingIntent pendingIntent = PendingIntent.getActivity(currentActivity, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =new  NotificationChannel(mHighChannelId, mHighChannelName, NotificationManager.IMPORTANCE_HIGH);
            channel.setShowBadge(true);
            mManager.createNotificationChannel(channel);
        }
        mBuilder = new NotificationCompat.Builder(currentActivity, mHighChannelId)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(res)
                .setLargeIcon(BitmapFactory.decodeResource(currentActivity.getResources(),res))
                .setAutoCancel(true)
//                .setNumber(999) // 自定义桌面通知数量
//                .addAction(res, "去看看", pendingIntent)// 通知上的操作
                .setCategory(NotificationCompat.CATEGORY_MESSAGE) // 通知类别，"勿扰模式"时系统会决定要不要显示你的通知
                .setVisibility(NotificationCompat.VISIBILITY_PRIVATE); // 屏幕可见性，锁屏时，显示icon和标题，内容隐藏

        if(currentActivity!=null&&toClass!=null){
            Intent intent = new Intent(currentActivity, toClass);
            PendingIntent pendingIntent = PendingIntent.getActivity(currentActivity, 0, intent, PendingIntent.FLAG_IMMUTABLE);
            mBuilder.addAction(res, "去看看", pendingIntent);// 通知上的操作)
        }



        mManager.notify(mHighNotificationId, mBuilder.build());
    }

    public static void  createNotificationForBigText(Activity currentActivity,int res
            ,String title,String content) {
        if(mManager==null){
            mManager= (NotificationManager) currentActivity.getSystemService(NOTIFICATION_SERVICE);
        }
        String bigText =content;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =new  NotificationChannel(mBigTextChannelId, mBigTextChannelName, NotificationManager.IMPORTANCE_DEFAULT);
            mManager.createNotificationChannel(channel);
        }
        mBuilder = new NotificationCompat.Builder(currentActivity, mBigTextChannelId)
            .setContentTitle(title)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
                .setSmallIcon(res)
                .setLargeIcon(BitmapFactory.decodeResource(currentActivity.getResources(), res))
                .setAutoCancel(true);
        mManager.notify(mBigTextNotificationId, mBuilder.build());
    }

    public static void createNotificationForBigImage(Activity currentActivity,int picRes,int res
            ,String title,String content) {
        if(mManager==null){
            mManager= (NotificationManager) currentActivity.getSystemService(NOTIFICATION_SERVICE);
        }
        Bitmap bigPic = BitmapFactory.decodeResource(currentActivity.getResources(), picRes);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel =new NotificationChannel(mBigImageChannelId, mBigImageChannelName, NotificationManager.IMPORTANCE_DEFAULT);
            mManager.createNotificationChannel(channel);
        }
        mBuilder =new NotificationCompat.Builder(currentActivity, mBigImageChannelId)
            .setContentTitle(title)
                .setContentText(content)
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bigPic))
                .setSmallIcon(res)
                .setLargeIcon(BitmapFactory.decodeResource(currentActivity.getResources(), res))
                .setAutoCancel(true);
        mManager.notify(mBigImageNotificationId, mBuilder.build());
    }


}
